package com.sl.plat.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sl.common.ResponseData;
import com.sl.model.enums.RehearsalSchemeTypeEnum;
import com.sl.water.vo.FastRehearsalVO;
import com.sl.water.vo.RehearsalParamsVO;
import com.sl.water.vo.RehearsalSchemeIVO;
import com.sl.water.entity.RehearsalScheme;
import com.sl.water.service.RehearsalSchemeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/plat/rehearsal")
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
@Tag(name = "预演-预演方案", description = "提供预演方案维护相关功能")
public class RehearsalController {

    private final RehearsalSchemeService rehearsalSchemeService;

    @GetMapping("/bfhFinishedPageList")
    @Operation(summary = "获取自动跟人工不分洪执行完成的预演方案分页列表")
    public ResponseData<Page<RehearsalScheme>> getBfhFinishedList(
            @Parameter(
                    description = "页码",
                    example = "1", required = true
            )
            @RequestParam(defaultValue = "1") int page,
            @Parameter(
                    description = "分页数量",
                    example = "10", required = true
            )
            @RequestParam(defaultValue = "10") int size,
            @Parameter(
                    description = "名称"
            ) @RequestParam(required = false) String name) {
        Page<RehearsalScheme> rehearsalSchemePage = rehearsalSchemeService.getFinishedList(page, size, name);
        return ResponseData.success(rehearsalSchemePage);
    }

    @GetMapping("/pageList")
    @Operation(summary = "分页列表")
    public ResponseData<Page<RehearsalScheme>> pageList(
            @Parameter(
                    description = "页码",
                    example = "1", required = true
            )
            @RequestParam(defaultValue = "1") int page,
            @Parameter(
                    description = "分页数量",
                    example = "10", required = true
            )
            @RequestParam(defaultValue = "10") int size,
            @Parameter(
                    description = "预演类型:0人工预演，1自动预演，2典型场景,-1 快速预演"
            ) @RequestParam(required = false) Integer type,
            @Parameter(
                    description = "不分洪状态，0为等待，1为计算中，2为计算完成，3为失败"
            ) @RequestParam(required = false) Integer status1,
            @Parameter(
                    description = "分洪状态,0为等待，1为计算中，2为计算完成，3为失败"
            ) @RequestParam(required = false) Integer status2) {
        Page<RehearsalScheme> rehearsalSchemePage = rehearsalSchemeService.pageList(page, size, type, status1, status2);
        return ResponseData.success(rehearsalSchemePage);
    }

    @GetMapping
    @Operation(summary = "列表")
    public ResponseData<List<RehearsalScheme>> getList(@Parameter(
            description = "预演类型(0人工预演，1自动预演，2典型场景)",
            example = "0"
    )
                                                       @RequestParam(required = false) Integer type, @Parameter(
            description = "不分洪状态，0为等待，1为计算中，2为计算完成，3为失败",
            example = "0"
    )
                                                       @RequestParam(required = false) Integer status1, @Parameter(
            description = "分洪状态，0为等待，1为计算中，2为计算完成，3为失败",
            example = "0"
    )
                                                       @RequestParam(required = false) Integer status2) {
        return ResponseData.success(rehearsalSchemeService.listByType(type, status1, status2));
    }

    @PostMapping
    @Operation(summary = "新增")
    public ResponseData save(@RequestBody RehearsalSchemeIVO rehearsalSchemeVO) {
        RehearsalScheme rehearsalScheme = new RehearsalScheme();
        BeanUtil.copyProperties(rehearsalSchemeVO, rehearsalScheme);
        rehearsalScheme.setType(RehearsalSchemeTypeEnum.ARTIFICIAL.getValue());
        rehearsalScheme.setStatus1(0);
        rehearsalScheme.setStatus2(0);
        rehearsalSchemeService.save(rehearsalScheme);
        return ResponseData.success(rehearsalScheme.getId());
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除")
    public ResponseData removeById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        rehearsalSchemeService.removeScheme(id);
        return ResponseData.success("删除成功");
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改")
    public ResponseData updateById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id,
            @RequestBody RehearsalSchemeIVO rehearsalSchemeVO) {
        RehearsalScheme rehearsalScheme = new RehearsalScheme();
        BeanUtil.copyProperties(rehearsalSchemeVO, rehearsalScheme);
        rehearsalScheme.setId(id);
        rehearsalSchemeService.updateById(rehearsalScheme);
        return ResponseData.success("修改成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询")
    public ResponseData<RehearsalScheme> get(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        return ResponseData.success(rehearsalSchemeService.getById(id));
    }

    @GetMapping("/run_bfh/{id}")
    @Operation(summary = "运行不分洪模型")
    public ResponseData runBfh(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        RehearsalScheme rehearsalScheme = rehearsalSchemeService.getScheme(id);
        rehearsalSchemeService.runModelCall(rehearsalScheme, "bfh", false);
        return ResponseData.success("执行中");
    }

    @GetMapping("/run_fh/{id}")
    @Operation(summary = "运行分洪模型")
    public ResponseData runFh(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        RehearsalScheme rehearsalScheme = rehearsalSchemeService.getScheme(id);
        rehearsalSchemeService.runModelCall(rehearsalScheme, "fh", false);
        return ResponseData.success("执行中");
    }

    @PostMapping("/run_fast")
    @Operation(summary = "快速预演，返回方案ID")
    public ResponseData runFast(@RequestBody FastRehearsalVO fastRehearsalVO) {
        return ResponseData.success(rehearsalSchemeService.runFast(fastRehearsalVO));
    }

    @GetMapping("/cunList/{id}")
    @Operation(summary = "按照ID,分洪类型查询村庄结果")
    public ResponseData cunList(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id, @Parameter(
            description = "类型，bfh 不分洪，fh 分洪",
            required = true,
            example = "bfh"
    )
            @RequestParam String kind) {
        return ResponseData.success(rehearsalSchemeService.cunList(id, kind));
    }

    @GetMapping("/dmList/{id}")
    @Operation(summary = "按照ID,分洪类型查询断面结果")
    public ResponseData dmList(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id, @Parameter(
            description = "类型，bfh 不分洪，fh 分洪",
            required = true,
            example = "bfh"
    )
            @RequestParam String kind) {
        return ResponseData.success(rehearsalSchemeService.dmList(id, kind));
    }

    @GetMapping("/yldList/{id}")
    @Operation(summary = "按照ID,分洪类型查询易涝点结果")
    public ResponseData yldList(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id, @Parameter(
            description = "类型，bfh 不分洪，fh 分洪",
            required = true,
            example = "bfh"
    )
            @RequestParam String kind) {
        return ResponseData.success(rehearsalSchemeService.yldList(id, kind));
    }

    @GetMapping("/imageList/{id}")
    @Operation(summary = "按照ID,分洪类型查询图片")
    public ResponseData imageList(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id, @Parameter(
            description = "类型，bfh 不分洪，fh 分洪",
            required = true,
            example = "bfh"
    )
            @RequestParam String kind) {
        RehearsalScheme scheme = rehearsalSchemeService.getScheme(id);
        return ResponseData.success(rehearsalSchemeService.imageList(scheme, kind));
    }

    @GetMapping(value = "/imagePreview")
    @Operation(summary = "模型图片浏览")
    public ResponseEntity<byte[]> imagePreview(@Parameter(
            description = "图片路径",
            required = true,
            example = "1/bfh/6/gray.png"
    ) @RequestParam String filePath) {
        return rehearsalSchemeService.imagePreview(filePath);
    }

    @GetMapping("/fhStatus/{id}")
    @Operation(summary = "根据方案ID查询分洪状态")
    public ResponseData<Integer> getFhStatus(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        RehearsalScheme rehearsalScheme = rehearsalSchemeService.getById(id);
        if (rehearsalScheme == null) {
            throw new RuntimeException("预演方案不存在");
        }
        return ResponseData.success(rehearsalScheme.getStatus2());
    }

    @GetMapping("/bfhStatus/{id}")
    @Operation(summary = "根据方案ID查询不分洪状态")
    public ResponseData<Integer> getBfhStatus(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        RehearsalScheme rehearsalScheme = rehearsalSchemeService.getById(id);
        if (rehearsalScheme == null) {
            throw new RuntimeException("预演方案不存在");
        }
        return ResponseData.success(rehearsalScheme.getStatus1());
    }

    @GetMapping("/params/{id}")
    @Operation(summary = "根据方案ID查询参数")
    public ResponseData<RehearsalParamsVO> getRehearsalParams(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        return ResponseData.success(rehearsalSchemeService.getRehearsalParamsById(id));
    }
}